package Game.GameStates 
{
	import Common.Animation;
	import Common.Input;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import Game.Windows.DebugWindow;
	/**
	 * ...
	 * @author Yevgeniy Logachev
	 */
	public class MainMenuState extends BaseState
	{
		[Embed(source="../../../res/swf/mainMenu.swf", symbol="MainMenu")]
		public static var MenuClass:Class;
		
		[Embed(source="../../../res/swf/Explosion.swf", symbol="Explosion1")]
		public static var Anim:Class;
		
		private var m_mainMenu:Sprite 	= null;
		private var m_cursor:MenuCursor	= null;
		
		private var m_currentItem:int	= 0;
		private var m_numItems:int		= 4;
		private var m_textSpace:int		= 60;
		
		private var m_textColor:uint	= 0x000000;
				
		public function MainMenuState() 
		{
			super();
			
			m_mainMenu = new MenuClass();
			
			var textField:TextField = m_mainMenu.getChildByName("Item" + (m_currentItem + 1).toString()) as TextField;
			m_textColor = textField.textColor;
			m_cursor = new MenuCursor(textField.width);
			//m_cursor.x = Settings.screenWidth / 2;
			refreshCursor();
			m_mainMenu.addChild(m_cursor);
			
			m_mainMenu.x = Settings.screenWidth / 2;
			m_mainMenu.y = Settings.screenHeight / 2;
			
			Main.mainSprite.addChild(m_mainMenu);
			
			Main.mainSprite.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
			m_isInit = true;
		}	
		
		private function refreshCursor():void
		{
			var textField:TextField;
			for (var i:int = 0; i < m_numItems; ++i)
			{
				textField = m_mainMenu.getChildByName("Item" + (i + 1).toString()) as TextField;
				textField.textColor = m_textColor;
			}
			
			m_cursor.y = m_currentItem * m_textSpace;
			textField = m_mainMenu.getChildByName("Item" + (m_currentItem + 1).toString()) as TextField;
			textField.textColor = 0x666666;
			m_cursor.setWidth(textField.width);
			
			
		}
		
		public function onKeyDown(e:KeyboardEvent):void
		{
			//trace("Key code: " + e.keyCode);
			if (e.keyCode == Input.KEY_DOWN)
			{
				if (++m_currentItem >= m_numItems)
				{
					m_currentItem = 0;
				}
			}
			if (e.keyCode == Input.KEY_UP)
			{
				if (--m_currentItem < 0)
				{
					m_currentItem = m_numItems - 1;
				}
			}
			if (e.keyCode == Input.KEY_ENTER)
			{
				switch(m_currentItem)
				{
					case 0:
						Main.startGame();
					break;
					
					case 1:
						Main.startEditor();
					break;
				}				
			}
			if (e.keyCode == Input.KEY_ESC)
			{
				DebugWindow.instance.show = !DebugWindow.instance.show;
			}
			
			refreshCursor();
		}
		
		override public function update():void
		{
		}
	}
}


import flash.display.Sprite;

class MenuCursor extends Sprite
{
	[Embed(source="../../../res/swf/mainMenu.swf", symbol="arowAnim")]
	public static var ArrowClass:Class;
	
	private var m_leftArrow:Sprite 		= null;
	private var m_rightArrow:Sprite 	= null;
	
	private var m_cursorShift:int		= 70;
	
	public function MenuCursor(cursorWidth:int) 
	{
		super();
		
		m_leftArrow = new ArrowClass();
		m_rightArrow = new ArrowClass();
		m_rightArrow.rotation = 180;
		
		m_leftArrow.x = -(m_cursorShift + cursorWidth / 2);
		m_rightArrow.x = m_cursorShift + cursorWidth / 2;
		
		addChild(m_leftArrow);
		addChild(m_rightArrow);
	}
	
	public function setWidth(cursorWidth:int):void
	{
		m_leftArrow.x = -(m_cursorShift + cursorWidth / 2);
		
		m_rightArrow.x = m_cursorShift + cursorWidth / 2;
	}
}